package com.hong;

import com.hong.java.client.testapp.HelloPrx;
import com.hong.java.client.testapp.HelloPrxCallback;
import com.qq.tars.client.Communicator;
import com.qq.tars.client.CommunicatorConfig;
import com.qq.tars.client.CommunicatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;
/**
 *
 * 功能描述: 测试可用
 *
 * @param:
 * @return:
 * @auther: csh
 * @date: 2021/3/5 17:34
 */
public class Main {

    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) throws InterruptedException {
        // Start configuration locally
        CommunicatorConfig cfg = new CommunicatorConfig();
        // Start communicator locally
        Communicator communicator = CommunicatorFactory.getInstance().getCommunicator(cfg);
        //warn If the deployment is started on the tars, you can only use the following constructor to get the communicator
        //Communicator communicator = CommunicatorFactory.getInstance().getCommunicator();
        //todo 注意这里的9002是你的服务端口
        //todo tarsjavaserver.javaserver.HelloObj 是你的服务的
        HelloPrx proxy = communicator.stringToProxy(HelloPrx.class, "tarsjavaserver.javaserver.HelloObj@tcp -h 127.0.0.1 -p 9002 -t 60000");

        //Synchronous call
        String ret = proxy.hello(1000, "Hello i'm hong!");
        System.out.println(ret);

        //One-way call
        proxy.async_hello(null, 1000, "Hello this is tars world! ");

        //Asynchronous call
        proxy.async_hello(new HelloPrxCallback() {

            @Override
            public void callback_expired() {
            }

            @Override
            public void callback_exception(Throwable ex) {
            }

            @Override
            public void callback_hello(String ret) {
                Main.logger.info("invoke use async {}", ret);

            }
        }, 1000, "Hello World2");

        proxy.promise_hello(1000, "hello world3").thenCompose(x -> {
            logger.info("invoke use promise {}", x);
            return CompletableFuture.completedFuture(0);
        });

        Thread.sleep(1000l);
    }
}